package com.xoms.front.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;
import com.xoms.front.cache.dao.ResourceCache;
import com.xoms.front.constants.Rescode;
import com.xoms.front.constants.RescodeConstants;
import com.xoms.front.controller.res.BaseResult;
import com.xoms.front.controller.res.bean.RAccountLoginBean;
import com.xoms.front.model.AccountBean;
import com.xoms.front.persist.dao.AccountDao;
import com.xoms.front.service.AccountService;
import com.xoms.front.service.exception.ServiceException;
import com.xoms.util.NumUtil;

public class AccountInterceptor extends HandlerInterceptorAdapter {
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	AccountService accountServiceImpl;

	@Autowired
	ResourceCache resourceCacheImpl;

	@Autowired
	AccountDao accountImpl;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		BaseResult result = new BaseResult();
		// 获取token
		String token = request.getHeader("token");
		if (token == null) {
			token = request.getParameter("token");
		}
		logger.info("account token is:" + token);
		try {
			RAccountLoginBean account = accountServiceImpl.getAccountByToken(token);
			if (account != null) {
				AccountBean acc = accountImpl.selectOneByAccid(account.getAccId());
				int deptId = NumUtil.toInt(acc.getDeptId(), 0);
				if(deptId != 34) {
					Rescode rescode = RescodeConstants.getInstance().get("account_need_allow");
					result.setFlag(false);
					result.setRescode(rescode.getCode());
					result.setMsg(rescode.getMsg());

					response.getWriter().write(JSON.toJSONString(result));
					return false;
					
				}
				request.setAttribute("accid", account.getAccId());
				request.setAttribute("account", acc);
				
				// TODO：验证权限地址
				// List<HaveResourcesBean> resources =
				// resourceCacheImpl.getAccountResource(account.getAccId());
				// if(null == resources || resources.size() == 0) {
				// // 无权限
				// Rescode rescode = RescodeConstants.getInstance().get("account_has_no_auth");
				// result.setFlag(false);
				// result.setRescode(rescode.getCode());
				// result.setMsg(rescode.getMsg());
				//
				// response.setCharacterEncoding("utf-8");
				// response.setContentType("application/json");
				// response.getWriter().write(JSON.toJSONString(result));
				// return false;
				// }

				logger.info("account accid is :" + account.getAccId());
			} else {
				Rescode rescode = RescodeConstants.getInstance().get("token_fail");
				result.setFlag(false);
				result.setRescode(rescode.getCode());
				result.setMsg(rescode.getMsg());

				response.getWriter().write(JSON.toJSONString(result));
				return false;
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
			result.setFlag(false);
			result.setRescode(e.getServiceCode());
			result.setMsg(e.getServiceMsg());

			response.getWriter().write(JSON.toJSONString(result));
			return false;
		}

		return super.preHandle(request, response, handler);
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}
